package com.ailanyin.admin.modules.system.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ailanyin.admin.modules.system.model.SysDictType;
import com.ailanyin.admin.modules.system.service.SysDictTypeService;
import com.ailanyin.common.result.AjaxResult;
import com.ailanyin.common.result.CommonPage;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统字典类型管理
 *
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/22 0022 上午 11:42
 */
@RestController
@RequestMapping("/system/dict/type")
@Api(tags = "系统字典类型管理")
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @PreAuthorize("hasAuthority('system:dict:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取字典类型列表")
    public CommonPage list(SysDictType dictType,
                           @RequestParam(value = "pageSize", required = false) Integer pageSize,
                           @RequestParam(value = "pageNum", required = false) Integer pageNum) {

        if (ObjectUtil.isNotEmpty(pageSize) && ObjectUtil.isNotEmpty(pageNum)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<SysDictType> list = sysDictTypeService.selectDictTypeList(dictType);
        return CommonPage.restPage(list);

    }

    @PreAuthorize("hasAuthority('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    @ApiOperation(value = "查询字典类型详细")
    public AjaxResult getInfo(@PathVariable Long dictId) {
        return AjaxResult.success(sysDictTypeService.selectDictTypeById(dictId));
    }

    @PreAuthorize("hasAuthority('system:dict:add')")
    @PostMapping
    @ApiOperation(value = "新增字典类型")
    public AjaxResult addDictType(@Validated @RequestBody SysDictType dict) {
        sysDictTypeService.addDictType(dict);
        return AjaxResult.success();
    }


    @PreAuthorize("hasAuthority('system:dict:edit')")
    @PutMapping
    @ApiOperation(value = "修改字典类型")
    public AjaxResult updateDictType(@Validated @RequestBody SysDictType dict) {
        sysDictTypeService.updateDictType(dict);
        return AjaxResult.success();
    }

    @PreAuthorize("hasAuthority('system:dict:remove')")
    @DeleteMapping("/{dictIds}")
    @ApiOperation(value = "删除字典类型")
    public AjaxResult remove(@PathVariable Long[] dictIds) {
        sysDictTypeService.deleteDictTypeByIds(dictIds);
        return AjaxResult.success();
    }

    @PreAuthorize("hasAuthority('system:dict:remove')")
    @DeleteMapping("/refreshCache")
    @ApiOperation(value = "刷新字典缓存")
    public AjaxResult refreshCache() {

        sysDictTypeService.resetDictCache();
        return AjaxResult.success();
    }

    @GetMapping("/optionselect")
    @ApiOperation(value = "获取字典选择框列表")
    public AjaxResult optionSelect() {
        List<SysDictType> dictTypes = sysDictTypeService.selectDictTypeAll();
        return AjaxResult.success(dictTypes);
    }
}
